namespace CodeCounsel.Office.WordSourceViewer
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Xml;
    using ICSharpCode.TextEditor;
    using ICSharpCode.TextEditor.Document;
 
    public class XmlEditorControl : TextEditorControl
    {
        public XmlEditorControl()
        {
            Document.DocumentChanged += Document_DocumentChanged;
            Document.FoldingManager.FoldingStrategy = new XmlFoldingStrategy();
            HighlightingManager.Manager.AddSyntaxModeFileProvider(
                new XmlSyntaxModeProvider());
            this.TextEditorProperties.ShowInvalidLines = false;
            Document.HighlightingStrategy = HighlightingManager.Manager.FindHighlighter("Xml");
        }

        void Document_DocumentChanged(object sender, DocumentEventArgs e)
        {
            Document.FoldingManager.UpdateFoldings("", null);
        }

        class XmlSyntaxModeProvider : ISyntaxModeFileProvider
        {
            public static readonly string _syntaxName = "Xml";

            List<SyntaxMode> _modes = new List<SyntaxMode>();

            public ICollection<SyntaxMode> SyntaxModes
            {
                get { return _modes; }
            }

            public XmlSyntaxModeProvider()
            {
                _modes.Add(new SyntaxMode("XmlHighlighting.xml", _syntaxName,
                    new string[] { "xml" }));
            }

            public XmlTextReader GetSyntaxModeFile(SyntaxMode syntaxMode)
            {
                XmlTextReader reader = null;
                if (syntaxMode.Name == _syntaxName)
                {
                    reader = new XmlTextReader(
                        Assembly.GetExecutingAssembly().GetManifestResourceStream(
                            "CodeCounsel.Office.WordSourceViewer.XmlEditor.XmlHighlighting.xml"));
                }
                return reader;
            }

            public void UpdateSyntaxModeList()
            {
            }
        }
    }
}
